﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using ZLCMS.Business.Util;
using System.Security.Cryptography;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace ZLCMS.Web.Tools
{
    public partial class CheckCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
            //Session[GlobalSessionString.UserCheckCode] = GenerateCheckCode();
            //this.CreateCheckCodeImage(Session[GlobalSessionString.UserCheckCode].ToString());

            this.CreateCheckCodeImage(CheckCodeKit.SetCheckCode());
        }

        private string GenerateCheckCode()
        {
            int number;
            char code;
            string checkCode = String.Empty;
            Random random = new Random();
            for (int i = 0; i < 5; i++)
            {
                number = random.Next();
                code = (char)('0' + (char)(number % 10));
                checkCode += code.ToString();
            }
            return checkCode;
        }

        private void CreateCheckCodeImage(string checkCode)
        {
            if (string.IsNullOrEmpty(checkCode))
                return;
            Color[] textColors = {
                Color.FromArgb(21, 89, 129),
                Color.FromArgb(11,163,53),  
                Color.FromArgb(203, 59, 3),
                Color.FromArgb(197,69,13),  
                Color.FromArgb(77, 123, 14)
            };
            var img = new VerifyImage().GenerateImage(checkCode, (int)Math.Ceiling(checkCode.Length * 12.5), 23, Color.White, textColors[new Random().Next(textColors.Length)]);
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/pjpg";
            Response.BinaryWrite(memoryStream.ToArray());

            img.Dispose();
            memoryStream.Dispose();
        }
    }

    /// <summary>
    /// 验证码图片类
    /// </summary>
    public class VerifyImage
    {
        private static Random random = new Random();
        private static byte[] randb = new byte[4];
        private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();

        private static Matrix m = new Matrix();
        private static Bitmap charbmp = new Bitmap(40, 40);

        private static Font[] fonts = {
                                        new Font(new FontFamily("Times New Roman"), 16 + random.Next(3),(System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)),
                                        new Font(new FontFamily("Georgia"), 16 + random.Next(3),(System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)),
                                        new Font(new FontFamily("Arial"), 16 + random.Next(3), (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)),
                                        new Font(new FontFamily("Comic Sans MS"), 16 + random.Next(3), (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))
                                     };

        public Bitmap GenerateImage(string code, int width, int height, Color bgcolor, Color textColor)
        {
            // 直接指定尺寸, 而不使用外部参数中的建议尺寸
            width = 120;
            height = 40;

            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            Graphics g = Graphics.FromImage(bitmap);
            g.SmoothingMode = SmoothingMode.HighSpeed;
            g.Clear(bgcolor);

            Pen linePen = new Pen(textColor);
            System.Drawing.Drawing2D.LinearGradientBrush drawBrush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, bitmap.Width, bitmap.Height), textColor, Color.Silver, 1.2f, true);

            for (int i = 0; i < 3; i++)
            {
                g.DrawArc(linePen, random.Next(20) - 10, random.Next(20) - 10, random.Next(width) + 10, random.Next(height) + 10, random.Next(-100, 100), random.Next(-200, 200));
            }

            Graphics charg = Graphics.FromImage(charbmp);

            float charx = -18;
            for (int i = 0; i < code.Length; i++)
            {
                m.Reset();
                m.RotateAt(random.Next(50) - 25, new PointF(random.Next(3) + 7, random.Next(3) + 7));

                charg.Clear(Color.Transparent);
                charg.Transform = m;

                charx = charx + 18 + random.Next(5);
                PointF drawPoint = new PointF(charx, 2.0F);
                charg.DrawString(code[i].ToString(), fonts[random.Next(fonts.Length - 1)], drawBrush, new PointF(0, 0));

                charg.ResetTransform();

                g.DrawImage(charbmp, drawPoint);
            }

            //画图片的前景噪音点
            for (int i = 0; i < 88; i++)
            {
                int x = random.Next(bitmap.Width);
                int y = random.Next(bitmap.Height);
                bitmap.SetPixel(x, y, Color.FromArgb(random.Next(0xffffff)));
            }

            drawBrush.Dispose();
            g.Dispose();
            charg.Dispose();

            return bitmap;
        }
    }
}
